<template>
	<div class="wrapper">
		<u-navbar class="navbar" :is-back="false" :is-fixed="true">
			<search v-if="!ZnavigationFlag" style="width: 80%;padding-top: 90px;" />
			<div slot="right" class="navbar-right1">
				<img src="../../../../static/tabbar/home-s1.png" @click="navigateTo('/pages/tabbar/home/index', 'switch')">
			</div>

			<!-- #ifndef H5 -->
			<!-- 扫码功能 不兼容h5 详情文档: https://uniapp.dcloud.io/api/system/barcode?id=scancode -->
			<div v-if="!ZnavigationFlag" slot="right" class="navbar-right2">
				<u-icon name="scan" @click="scan()" color="#666" size="50"></u-icon>
			</div>
			<!-- #endif -->
		</u-navbar>
		<Common></Common>
	</div>
</template>
  
<script>
// 引用组件
import common from "@/pages/tabbar/home/template/common2"; //导航栏模块
import tpl_search from "@/pages/tabbar/home/template/tpl_search2"; //搜索栏
// 结束引用组件
import { getFloorData } from "@/api/home"; //获取楼层装修接口
import permision from "@/js_sdk/wa-permission/permission.js"; //权限工具类
import config from "@/config/config";
import tpl_notice from "@/pages/tabbar/home/template/tpl_notice"; //标题栏模块
import tpl_promotions from "@/pages/tabbar/home/template/tpl_promotions_detail"; //标题栏模块

export default {
	data() {
		return {
			ZnavigationFlag: false,
			otherMenu: true,
			config,
			pageData: "", //楼层页面数据
			isIos: "",
			enableLoad: false, //触底加载 针对于商品模块
		};
	},
	components: {
		Common: common,
		search: tpl_search,
	},

	mounted() {
		var uniPageHead = document.querySelectorAll('.uni-page-head');
		console.log(uniPageHead);
		let uniPageHeadBtn1 = uniPageHead[0];
		// uniPageHeadBtn1.style.display = 'none';
		this.init();
		// #ifdef MP-WEIXIN
		// 小程序默认分享
		uni.showShareMenu({ withShareTicket: true });
		// #endif
	},

	methods: {
		navigateTo(url, type) {
			if (type === "switch") {
				uni.switchTab({
					url,
				});
			} else {
				uni.navigateTo({
					url,
				});
			}
		},
		/**打开瀑布流菜单**/
		openZnavigation() {
			this.ZnavigationFlag = true;
			this.otherMenu = false;
		},
		/**关闭瀑布流菜单**/
		closeZnavigation() {
			this.ZnavigationFlag = false;
			this.otherMenu = true;
		},
		/**
		 * 实例化首页数据楼层
		 */
		init() {
			// uni.hideTabBar();
			this.pageData = "";
			getFloorData().then((res) => {
				if (res.data.success) {
					const result = JSON.parse(res.data.result.pageData)
					this.pageData = result;
					if (result.list.length) {
						// 如果最后一个装修模块是商品模块的话 默认启用自动加载
						result.list[result.list.length - 1] ? result.list[result.list.length - 1].model == 'goods' ? this.enableLoad = true : '' : ''
					}
				}
			});
		},
		// 是否有网络链接
		isConnected(val) {
			val ? this.init() : ''
		},

		/**
		 * TODO 扫码功能后续还会后续增加
		 * 应该实现的功能目前计划有：
		 * 扫描商品跳转商品页面
		 * 扫描活动跳转活动页面
		 * 扫描二维码登录
		 * 扫描其他站信息 弹出提示，返回首页。
		 */
		seacnCode() {
			uni.scanCode({
				success: function (res) {
					let path = encodeURIComponent(res.result);



					if (path != undefined && path.indexOf("QR_CODE_LOGIN_SESSION") == 0) {
						console.log(path)
						//app扫码登录
						uni.navigateTo({
							url: "/pages/passport/scannerCodeLoginConfirm?token=" + path
						});
						return;
					}


					// WX_CODE 为小程序码
					if (res.scanType == "WX_CODE") {
						console.log(res);
						uni.navigateTo({
							url: `/${res.path}`,
						});
					} else {
						config.scanAuthNavigation.forEach((src) => {
							if (res.result.indexOf(src) != -1) {
								uni.navigateTo({
									url: `/${res.result.substring(src.length)}`,
								});
							} else {
								setTimeout(() => {
									uni.navigateTo({
										url: "/pages/tabbar/home/web-view?src=" + path,
									});
								}, 100);
							}
						});
					}
				},
			});
		},

		/**
		 * 提示获取权限
		 */
		tipsGetSettings() {
			uni.showModal({
				title: "提示",
				content: "您已经关闭相机权限,去设置",
				success: function (res) {
					if (res.confirm) {
						if (this.isIos) {
							plus.runtime.openURL("app-settings:");
						} else {
							permision.gotoAppPermissionSetting();
						}
					}
				},
			});
		},

		/**
		 * 唤醒客户端扫码
		 * 没权限去申请权限，有权限获取扫码功能
		 */
		async scan() {
			// #ifdef APP-PLUS
			this.isIos = plus.os.name == "iOS";
			// 判断是否是Ios
			if (this.isIos) {
				const iosFirstCamera = uni.getStorageSync("iosFirstCamera"); //是不是第一次开启相机
				if (iosFirstCamera !== "false") {
					uni.setStorageSync("iosFirstCamera", "false"); //设为false就代表不是第一次开启相机
					this.seacnCode();
				} else {
					if (permision.judgeIosPermission("camera")) {
						this.seacnCode();
					} else {
						// 没有权限提醒是否去申请权限
						this.tipsGetSettings();
					}
				}
			} else {
				/**
				 * TODO 安卓 权限已经授权了，调用api总是显示用户已永久拒绝申请。人傻了
				 * TODO 如果xdm有更好的办法请在 https://gitee.com/beijing_hongye_huicheng/lilishop/issues 提下谢谢
				 */
				this.seacnCode();
			}

			// #endif

			// #ifdef MP-WEIXIN
			this.seacnCode();
			// #endif
		},
	},
};
</script>
  
<style scoped lang="scss">
.navbar-right1 {
	padding: 100px 40px 0 0;
}

.navbar-right2 {
	padding: 100px 0px 0 0;
}
</style>
  